<%@ include file="/en/jsp/static.jsp" %>
<%@ page import="java.util.*,linuxbox.news.logic.*,linuxbox.BoxStatic" %>
<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>


<jsp:include page="/en/jsp/header.jsp" flush="true"/>

<%
  String VIEW_TYPE = (String) session.getAttribute("news.VIEW_TYPE");
  if (VIEW_TYPE == null) {
    VIEW_TYPE = BoxNews2.VT_ALL;
    session.setAttribute("news.VIEW_TYPE", VIEW_TYPE);
  }

  String SORTBY = (String) session.getAttribute("news.SORTBY");
  if (SORTBY == null) {
    SORTBY = BoxNews2.ORDER_BY_SUBTIME;
    session.setAttribute("news.SORTBY", SORTBY);
  }

  boolean sortAsc = true;
  String tmp = (String) session.getAttribute("news.ASC");
  if (tmp!=null && tmp.length() > 0)
    sortAsc = new Boolean(tmp).booleanValue();
  else
    session.setAttribute("news.ASC", "true");

  int curPage = 0;
  String PAGE = (String) request.getParameter("PAGE");
  if (PAGE != null)
    curPage = Integer.parseInt(PAGE);

  BoxNews2 home = BoxNews2.getInstance();
  int entriesPerPage = sec.getConfigInt("news", "Admin", "Entries Per Page");
  int totalPage = home.getTotalPages(entriesPerPage, VIEW_TYPE);
  if (curPage >= totalPage)
    curPage = totalPage - 1;

  List<BoxNewsEntry> it = home.getNews(VIEW_TYPE, SORTBY, sortAsc, 
    entriesPerPage * curPage, entriesPerPage);
  String actionJSP = BoxStatic.jspDir + "/news/BoxNewsMt.jsp";
  int count = 0;
%>

<script Language="JavaScript">
  function confirmDel(entry) {
    return confirm("Are you sure to delete " + entry);
  }
  function onChangeViewType() {
    document.theForm.submit();
  }
</script>

<FORM action="<%=BoxStatic.servletDir%>BoxNewsAction" name="theForm" method="POST">
  <table width="100%" height="26" align="middle" border="0" cellpadding="0"
    cellspacing="0" bgcolor="#f4f4f4" style="font-size:16px;color:#d05000">
    <tr>
      <td>
          &nbsp;<img src="<%=BoxStatic.imageDir%>itemball.jpg" width="14"
                  height="15" border="0" align="absmiddle">
          <b><%=boxName%> - News Maintenance</b>
      </td>
      <td align=right>
        <a href="<%=BoxStatic.servletDir%>BoxNewsAdminSettingsAction?SubmitEdit=edit">
          Settings
        </a>
      </td>
    </tr>
  </table>

  <hr width="100%" color="#E06000" align="left">

  <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="0">
    <tr><td>
      <table width="100%">
        <tr>
          <td>
            <input type="submit" name="SubmitEdit" value="Add...">
          </td>
          <td>View
              <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
                <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
                <option <% if (VIEW_TYPE.compareTo(home.VT_CURRENT) == 0) out.print(" selected ");%> value="<%=home.VT_CURRENT%>">Current</option>
                <option <% if (VIEW_TYPE.compareTo(home.VT_EXPIRED) == 0) out.print(" selected ");%> value="<%=home.VT_EXPIRED%>">Expired</option>
              </select>
          </td>
          <td align=right>
            <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=actionJSP%>" />
          </td>
        </tr>
      </table>
    </td></tr>

    <TR bgcolor=silver>
      <td>
        <TABLE WIDTH="100%" BORDER=0 CELLSPACING=1 CELLPADDING=0 bgColor=#dddddd>
          <%
            if ( it != null) {
              out.print("<tr bgcolor=#7EC1F3>");
              String[] headers = {"Submit Time", "Expire Time", "Subject", "Delete"};
              String[] sortby = {"subtime", "exptime", "subject", ""};
              for(int i = 0; i < headers.length; i++) {
                if (sortby[i].length() > 0) {
                  out.print("<TH><a href=\"");
                  out.print(BoxStatic.servletDir);
                  out.print("/BoxNewsAction?SORTBY=");
                  out.print(sortby[i]);
                  out.print("&ASC=");
                  out.print(!sortAsc);
                  out.print("\">");
                  out.print(headers[i]);
                  out.print("&nbsp;");
                  if (SORTBY.compareTo(sortby[i]) == 0) {
                    if (sortAsc)
                      out.print("<img SRC=\"" +BoxStatic.imageDir+ "asc.gif\" border=0>");
                    else
                      out.print("<img SRC=\"" +BoxStatic.imageDir+ "desc.gif\" border=0>");
                  }
                  out.print("</a></TH>");
                } else {
                  out.print("<TH>");
                  out.print(headers[i]);
                  out.print("</TH>");
                }
              }
              out.print("</tr>");
%>
              <common:Iteration name="news"
                type="linuxbox.news.logic.BoxNewsEntry" 
                group="<%=it.iterator()%>">
              <tr <% if ((count++ % 2) == 0) out.print("bgcolor=#f0f0f0"); %>>
                <td><jsp:getProperty name="news" property="subTimeString"/></td>
                <td><jsp:getProperty name="news" property="expTimeString"/></td>
                <td><a href="<%=BoxStatic.servletDir%>BoxNewsAction?SubmitEdit=Edit&ID=<%=news.getId()%>">
                    <jsp:getProperty name="news" property="subject"/></a>
                </td>
                <td align=center><a
href="<%=BoxStatic.servletDir%>BoxNewsAction?SubmitDelete=Delete&ID=<%=news.
getId()%>">Delete</a></td>
              </tr>
              </common:Iteration>
<%
            } else {
              out.print("<TR><TD bgColor=\"#eeeeee\" align=\"center\">"
                        + "There is no news entries.</TD></TR>");
            }
          %>
        </TABLE>
      </td>
    </tr>
  <%
    if (count > 0) {
  %>
    <tr><td>
      <table width="100%">
        <tr>
          <td>
            <input type="submit" name="SubmitEdit" value="Add...">
          </td>
          <td>View
              <select name="VIEW_TYPE" size="1" onchange="onChangeViewType()">
                <option <% if (VIEW_TYPE.compareTo(home.VT_ALL) == 0) out.print(" selected ");%> value="<%=home.VT_ALL%>">All</option>
                <option <% if (VIEW_TYPE.compareTo(home.VT_CURRENT) == 0) out.print(" selected ");%> value="<%=home.VT_CURRENT%>">Current</option>
                <option <% if (VIEW_TYPE.compareTo(home.VT_EXPIRED) == 0) out.print(" selected ");%> value="<%=home.VT_EXPIRED%>">Expired</option>
              </select>
          </td>
          <td align=right>
            <common:Page curPage="<%=curPage%>" totalPage="<%=totalPage%>" action="<%=actionJSP%>" />
          </td>
        </tr>
      </table>
    </td></tr>
  <%
    }
  %>
    <tr><td align=center>
    <hr width="100%" color=#e0e0e0 align=left>
    </td></tr>

    <tr><td align=center>
      <table cellSpacing=1 cellPadding=1  border=0 bgColor=#eeeeee>
        <tr bgcolor=#d4d4d4><td align=center>
        <b>Quick Add News</b>
        </td></tr>
        <tr><td align=center>
          <table>
            <tr>
              <td>
                <b>Subject:</b> <input align=right type=text name=SUBJECT size=30 value="">&nbsp;&nbsp; <input type="submit" name="SubmitSave" value="Add">
              </td>
            </tr>
            <tr>
              <td>
                <b>Content:</b> <textarea name=CONTENT rows=3 cols=50></textarea><br>
              </td>
            </tr>
          </table>
        </td></tr>
      </table>
    </td></tr>

  </table>
  <hr width="100%" color=#E06000 align=left>

<jsp:include page="/en/jsp/footer.jsp" flush="true"/>
